package com.example.treehole;

import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.example.treehole.base.BaseBindingActivity;
import com.example.treehole.base.BindAdapter;
import com.example.treehole.bean.Chat;
import com.example.treehole.bean.Message;
import com.example.treehole.databinding.ActivityChatBinding;
import com.example.treehole.databinding.ItemChatBinding;
import com.example.treehole.util.Api;
import com.example.treehole.util.HttpUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


public class ChatActivity extends BaseBindingActivity<ActivityChatBinding> {
    private BindAdapter<ItemChatBinding, Message> adapter = new BindAdapter<ItemChatBinding, Message>() {
        @Override
        public ItemChatBinding createHolder(ViewGroup parent) {
            return ItemChatBinding.inflate(getLayoutInflater(), parent, false);
        }

        @Override
        public void bind(ItemChatBinding item, Message message, int position) {
            if (message.userid.equals(App.user.id)) {
                item.left.setVisibility(View.GONE);
                item.right.setVisibility(View.VISIBLE);
                item.tvMessage.setText(message.content);
                loadImage(message.user.face, item.ivFace);
            } else {
                item.right.setVisibility(View.GONE);
                item.left.setVisibility(View.VISIBLE);
                item.tvMessage1.setText(message.content);
                loadImage(message.user.face, item.ivFace1);
            }
        }
    };

    @Override
    protected void initListener() {

    }

    Chat chat;

    @Override
    protected void initData() {
        chat = (Chat) getIntent().getSerializableExtra("chat");
        viewBinder.tvTitle.setText(chat.other.nickname);
        viewBinder.rvMessage.setAdapter(adapter);
        viewBinder.tvSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = viewBinder.etMessage.getText().toString();
                if (content.isEmpty()) return;
                Message message = new Message();
                message.userid = App.user.id;
                message.chatId = chat.id;
                message.toId = App.user.id.equals(chat.userid1) ? chat.userid2 : chat.userid1;
                message.content = content;
                message.time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                message.user = App.user;
                adapter.getData().add(message);
                adapter.notifyDataSetChanged();
                viewBinder.etMessage.setText("");
                HttpUtil.post(Api.CHAT_SEND_MESSAGE, HttpUtil.toJSON(message), new HttpUtil.OnNetStringListener() {
                    @Override
                    public void success(String data, String message) {
                        toast(message);
                    }

                    @Override
                    public void error(String message) {
                        toast(message);
                    }
                });
            }
        });
        refresh();
    }

    private void refresh() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("chatId", chat.id);
        params.put("userid", App.user.id);
        HttpUtil.getList(Api.CHAT_GET_MESSAGE, params, Message.class, new HttpUtil.OnNetListListener<Message>() {
            @Override
            public void success(List<Message> data, String message) {
                if (isDestroyed()) return;
                adapter.getData().clear();
                adapter.getData().addAll(data);
                adapter.notifyDataSetChanged();
                handler.sendEmptyMessageDelayed(1, 2000);
            }

            @Override
            public void error(String message) {
                toast(message);
            }
        });
    }

    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull android.os.Message msg) {
            super.handleMessage(msg);
            if (isDestroyed()) {
                return;
            }
            refresh();
        }
    };
}